package tzg.servlet;

import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
import tzg.bean.Page;
import tzg.bean.User;
import tzg.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

@WebServlet(urlPatterns ={"/user.Servlet"})
public class UserServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String param=request.getParameter("param");
        UserService userService=new UserService();
        String currentPage=null==request.getParameter("currentPage")?"1":request.getParameter("currentPage");
        Page<List<Map<String,Object>>> page=new Page<>(Integer.valueOf(currentPage));
        PrintWriter pw=response.getWriter();
        switch (param){
            case "login":
                String name=request.getParameter("name");
                String passwd=request.getParameter("passwd");
                String isCookie=request.getParameter("isCookie");
                if (isCookie!=null){
                    Cookie cookie=new Cookie("firstCookie",name);
                    cookie.setMaxAge(10);
                    response.addCookie(cookie);
                }
                HttpSession session=request.getSession();
                session.setAttribute("name",name);

                if ("admin".equals(name)&&"admin".equals(passwd)){

                    request.getRequestDispatcher("view/show.jsp").forward(request,response);
                }else {
                    request.getRequestDispatcher("login.jsp").forward(request,response);
                }
                break;
            case "register":
                String[] hobby=request.getParameterValues("hobby");
                String hobbies=String.join(",",null==hobby?new String[0]:hobby);
                    System.out.println(hobbies);
                break;
            case "table":
                String keyword=null==request.getParameter("keyword")?"":request.getParameter("keyword");
                String addFlag=request.getParameter("addFlag");

                if(addFlag!=null){
                    request.setAttribute("addFlag",addFlag);
                }
                page.setKeyword(keyword);
                userService.queryAllUserDetial(page);
                request.setAttribute("page",page);
                request.getRequestDispatcher("view/table.jsp").forward(request,response);
                break;
            case "deleteUser":
                int id=Integer.valueOf(request.getParameter("uid"));
                if(userService.deleteUser(id)){
                    pw.flush();
                    pw.print("<script>alert('删除成功！');location.href='user.Servlet?param=table'</script>");
                    pw.close();
                }else {
                    pw.flush();
                    pw.print("<script>alert('删除失败！');location.href='user.Servlet?param=table'</script>");
                    pw.close();
                }
                System.out.println(id);
                break;
            case "add":
                String array=request.getParameter("array");
                String[] user_content=array.split(",");
                for(String str:user_content){
                    System.out.println(str);
                }

                User user=new User(user_content[0],user_content[1],user_content[2],user_content[3],user_content[4],user_content[5]);
                break;
        }


    }
}
